package com.yuyang.gulimall.seckill.schedule;

import com.yuyang.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author yu yang
 * @date 2023/2/13 -15:24
 */
@Slf4j
@Component
public class SeckillSkuSchedule {
    @Autowired
    SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;
    private  final String  upload_lock = "seckill:upload:lock";
    //TODO 幂等性处理
//    @Scheduled(cron = "*/3 * * * * ?") //每3秒执行一次
        @Scheduled(cron = "0 0 3 * * ?") //线上模式 每晚3点执行一次
    public void uploadSeckillSkuLatest3Days(){
        log.info("定时任务执行缓存秒杀商品");
        RLock lock = redissonClient.getLock(upload_lock);
        try {
            lock.lock();
            seckillService.uploadSeckillSkuLatest3Days();
        }finally {
            lock.unlock();
        }

    }

}
